package org.tity.commons.image;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

 
/**
 * @author LeeFangJie
 */

public class SmartImageScalr {
	
	private BufferedImage source;
	private int height;
	private int width;
	private Scalr.Mode fitType=Scalr.Mode.AUTOMATIC;
	private String crop;
	
	/**
	 * 构造函数
	 * @param width 目标宽度
	 * @param height  目标高度
	 * @param fitType	缩放模式
	 * @param crop 是否裁切
	 * @param source	图像
	 */
	public SmartImageScalr(int width,int height,String fitType,String crop,BufferedImage source)
	{
		 this.source=source;
		 init(width,height,fitType,crop,this.source.getWidth(),this.source.getHeight());
	}
	
	private static final String WIDTH="width";
	private static final String HEIGHT="height";
 
	public void init(int width,int height,String fitType,String crop,double origWidth,double origHeight)
	{
		 this.width=width;
		 this.height=height;
		 this.crop=checkCropType(crop);

		if (WIDTH.equals(fitType)) {
			this.fitType = Scalr.Mode.FIT_TO_WIDTH;
			if(width<=0)
			{
				throw new IllegalArgumentException("当指定适应宽度，但宽度值却未指定");
			}
		} else if (HEIGHT.equals(fitType)) {
			this.fitType = Scalr.Mode.FIT_TO_HEIGHT;
			if(height<=0)
			{
				throw new IllegalArgumentException("当指定适应宽度，但宽度值却未指定");
			}
		}
		
		
		 
		int thumbWidth=0;
		int thumbHeight=0;
		if(this.fitType==Scalr.Mode.FIT_TO_WIDTH)
		{
			thumbWidth=width;
			thumbHeight=(int)((origHeight/origWidth)*thumbWidth);
			if(thumbHeight<height)
			{
				this.fitType = Scalr.Mode.FIT_TO_HEIGHT;
				if(height<=0)
				{
					throw new IllegalArgumentException("需要同时指定高度值才能操作");
				}
				return;
			}
		}
		else if(this.fitType==Scalr.Mode.FIT_TO_HEIGHT)
		{
			thumbHeight=height;
			thumbWidth=(int)((origWidth/origHeight)*thumbHeight);
			if(thumbWidth<width)
			{
				this.fitType = Scalr.Mode.FIT_TO_WIDTH;
				if(width<=0)
				{
					throw new IllegalArgumentException("需要同时指定宽度值才能操作");
				}
				return;
			}
		}
		
		
		 
	 }
	 
	 public String getFileName(File srcFile,String format)
	 {
		 String thumbFileName=srcFile.getName();
		 
		  
		 
		
		if (fitType==Scalr.Mode.FIT_TO_WIDTH) {
			thumbFileName += "_fw"+width;
		} else if (fitType==Scalr.Mode.FIT_TO_HEIGHT) {
			thumbFileName += "_fh"+height;
		}
		
		if(crop!=null && crop.length()>0)
		{
			thumbFileName +="cr"+crop.toLowerCase();
		}
		if(format==null) {
			format="jpg";
		}
		thumbFileName+="."+format;
		
		return thumbFileName;
	 }
	 
	 public  BufferedImage scaleAndCrop() throws IOException {
		return  scaleAndCrop(this.source); 
	 }
	
	
	public  BufferedImage scaleAndCrop(BufferedImage source) throws IOException {
		 
		this.source=source;
		
		BufferedImage thumb=null;
	 
		
		int origWidth=source.getWidth();
		int origHeight=source.getHeight();
 
		//如果尺寸和原始图片尺寸不一样，调整尺寸
		if(width!=origWidth || height!=origHeight)
		{
			//需要预先计算resize后的尺寸，在crop前反复resize存在性能问题
			thumb=Scalr.resize(source, Scalr.Method.QUALITY, fitType,width,height,Scalr.OP_ANTIALIAS);
		}
		else //否则不调整尺寸
		{
			thumb=source;
		}

		if(crop!=null)
		{
			Rectangle r=null;
			if(this.fitType==Scalr.Mode.FIT_TO_WIDTH)
			{
				if(height>0 && thumb.getHeight()<height)
				{
					thumb=Scalr.resize(source, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_HEIGHT,width,height,Scalr.OP_ANTIALIAS);
					r=getCropRectangle(crop,width,thumb.getHeight(),thumb.getWidth(),thumb.getHeight());
				}
				else
				{
					r=getCropRectangle(crop,thumb.getWidth(),height,thumb.getWidth(),thumb.getHeight());
				}
			}
			if(this.fitType==Scalr.Mode.FIT_TO_HEIGHT)
			{
				if(width>0 && thumb.getWidth()<width)
				{
					thumb=Scalr.resize(source, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH,width,height,Scalr.OP_ANTIALIAS);
					r=getCropRectangle(crop,thumb.getWidth(),height,thumb.getWidth(),thumb.getHeight());
				}
				else
				{
					r=getCropRectangle(crop,width,thumb.getHeight(),thumb.getWidth(),thumb.getHeight());
				}
			}		
			
			thumb=Scalr.crop(thumb,r.x, r.y,r.width,r.height,Scalr.OP_ANTIALIAS);
	
		}
		
		return thumb;
	}
	
	
	
	 
	
	
	
	private static Rectangle getCropRectangle(String crop, int width, int height,int imageWidth, int imageHeight) {
		Rectangle r=new Rectangle(0,0,width,height);
		int dw=imageWidth-width;
		int dh=imageHeight-height;
		
		if(TL.equals(crop))
		{
			r.x=0;
			r.y=0;
		}
		else if(TC.equals(crop) || TM.equals(crop))
		{
			r.x=dw/2;
			r.y=0;
		}
		else if(TR.equals(crop))
		{
			r.x=dw;
			r.y=0;
		}
		else if(RC.equals(crop) || RM.equals(crop))
		{
			r.x=dw;
			r.y=dh/2;
		}
		else if(RB.equals(crop))
		{
			r.x=dw;
			r.y=dh;
		}
		else if(BC.equals(crop) || BM.equals(crop))
		{
			r.x=dw/2;
			r.y=dh;
		}
		else if(BL.equals(crop))
		{
			r.x=0;
			r.y=dh;
		}
		else if(LM.equals(crop) || LC.equals(crop))
		{
			r.x=0;
			r.y=dh/2;
		}
		else if(MC.equals(crop))
		{
			r.x=dw/2;
			r.y=dh/2;
		}
		
		
		
		return r;
	}
	
	public static final String TL="TL";
	public static final String TM="TM";
	public static final String TC="TC";
	public static final String TR="TR";
	
	public static final String RM="RM";
	public static final String RC="RC";
	public static final String RB="RB";
	
	public static final String BC="BC";
	public static final String BM="BM";
	public static final String BL="BL";
	
	public static final String LM="LM";
	public static final String LC="LC";
	public static final String MC="MC";

	public static String[] CROPS={
			TL,TC,TM,TR
			,RC,RM,RB,
			BC,BM,BL,
			LM,LC,MC
	};
	
	private static final int CROP_NAME_LENGTH=2;
	
	private static String checkCropType(String type)
	{
		if(type==null) {
			return null;
		}
		type=type.trim();		
		if(type.length()!=CROP_NAME_LENGTH) {
			return null;
		}
		String type2=""+type.charAt(1)+type.charAt(0)+"";
		for (String c : CROPS) {
			if(c.equals(type) || type2.equals(c)) {
				return c;
			}
		}
		return null;
	}
	
	
	public static void main(String[] args) throws Exception {

	}

}

 
